﻿Imports System.Threading
''cambiar el thread.sleep
Public Class Animator

    Private Shared FRAME_PER_SEC = 230

    Private w As Stopwatch

    Public Sub New()
        w = New Stopwatch()
    End Sub

    Public Sub ClipHide(control As Control, Optional OnEnd As Action = Nothing)

        Dim heigth As Integer = control.Height
        While control.Height > 0
            Dim time As Double = w.Elapsed.TotalMilliseconds
            If time > (1000.0 / FRAME_PER_SEC) Then
                control.Height = control.Height - 25
                control.Refresh()
                w.Reset()
                w.Start()
            End If

        End While

        control.Hide()
        control.Height = heigth
        If OnEnd <> Nothing Then
            OnEnd()
        End If

    End Sub

    Public Sub ShowClip(control As Control)

        w.Reset()
        Dim heigth As Integer = control.Height
        control.Height = 0

        control.Show()
        w.Restart()
        While control.Height < heigth
            Dim time As Double = w.Elapsed.TotalMilliseconds
            If time > (1000.0 / FRAME_PER_SEC) Then
                control.Height = control.Height + 20
                control.Refresh()
                w.Reset()
                w.Start()
            End If


        End While

        control.Height = heigth

    End Sub

    Public Sub MoveCenter(control As Control)

        Move(control, (control.Parent.Width - control.Width) / 2, (control.Parent.Height - control.Height) / 2)

    End Sub
    Public Sub Move(control As Control, x As Integer, y As Integer)






        Try
            w.Restart()
            Dim incrment = 5
            Dim x1 As Integer = control.Location.X
            Dim y1 As Integer = control.Location.Y

            Dim c As Integer = x - x1
            Dim m As Integer = (y - y1) / c
            Dim b As Integer = y1 - (m * x1)

            Dim f As Action = If(c > 0, Sub() x1 += incrment, Sub() x1 -= incrment)
            Dim deviacion = (If(c > 0, -1, 1) * (Math.Abs(c) Mod incrment))

            w.Restart()
            While x1 <> x + deviacion

                Dim time As Double = w.Elapsed.TotalMilliseconds
                If time > (1000.0 / FRAME_PER_SEC) Then

                    f()
                    control.Location = New Point(x1, (x1 * m) + b)
                    w.Reset()
                    w.Start()

                End If
            End While
        Catch ex As OverflowException
            Console.WriteLine(ex.Message)




        End Try



    End Sub


    Public Function genericAnimationIterator(accion As Action) As Boolean



        Dim time As Double = w.Elapsed.TotalMilliseconds
        If time > (1000.0 / FRAME_PER_SEC) Then
            accion()
            w.Reset()
            w.Start()

        End If

        Return True

    End Function

End Class
